﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Net;
using System.IO;
namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for CKVN.
    /// </summary>
    public class MUZIK
    {
       
        private SMS_MO WMO;
        private SMS_MT WMT;
        //private SMS_LIST WML;
        ArrayList arr = new ArrayList();
        public MUZIK(SMS_MO MO)
        {
            WMO = MO;
        }

        public MUZIK()
        {
        }
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            if (msg.Length == 1)//Neu nhan tai ve mat
            {
                WMT.AddMessage(WMO.UserID, GetReturnMsg(msg), SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();
                return;
            }
            else if (msg.Length == 2)//Nhan tang cho thue bao khac      
            {
                //gửi tin nhắn thong bao cho người tặng----------------------------------------------------
                string strSender = "Ban vua gui tang so dien thoai " + msg[1];
                if (WMO.ServiceID.Replace("04", "") == "6072")//6575
                { strSender += " ca khuc " + msg[0] + ".Cam on ban da su dung dich vu cua tong dai 6x72"; }
                else if (WMO.ServiceID.Replace("04", "") == "6772")//6772
                { strSender += " bo nhac " + msg[0] + ".Cam on ban da su dung dich vu cua tong dai 6x72"; }                    
                else
                {
                    strSender = "De tai bai hat: Soan tin NC TenBaiHat gui 6072\nDe tang bai hat cho ban be: Soan tin NC TenBaiHat SoDienThoaiNhan gui 6072\nChi tiet tai: www.6x72.com.vn";
                }
                WMT.AddMessage(WMO.UserID, strSender, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();

                //Gưi tin nhắn noi dung cho người được tặng-----------------------------------------------
                string strReceived = "Mot nguoi o so dien thoai 0" + WMO.UserID.Substring(2, WMO.UserID.Length - 2) + " gui tang ban";
                if (WMO.ServiceID.Replace("04", "") == "6072")//6072
                { strReceived += " ca khuc " + msg[0]; }
                else if (WMO.ServiceID.Replace("04", "") == "6772")//6772
                { strReceived += " bo nhac " + msg[0]; }
                strReceived += ".\n";
                strReceived += GetReturnMsg(msg);
                //kiem tra mang di dong cua nguoi duoc tang
                string telco = CheckTelco(msg[1]);
                if (!string.IsNullOrEmpty(telco))
                {
                    //xac dinh server
                    string strconnec = "";
                    if (telco == "VIETTEL")
                    { strconnec = ConfigurationSettings.AppSettings["SMSConnStrViettel"].ToString(); }
                    else if (telco == "MOBI")
                    { strconnec = ConfigurationSettings.AppSettings["SMSConnStrMobi"].ToString(); }
                    else if (telco == "VINA")
                    { strconnec = ConfigurationSettings.AppSettings["SMSConnStrVina"].ToString(); }
                    //insert vao MT
                    InsertMT(strconnec, 0, "84" + msg[1].Trim().Substring(1, msg[1].Trim().Length - 1), WMO.ServiceID, WMO.CommandCode, strReceived, 0, 1, 0);
                    return;
                }
                else//Neu thue bao duoc tang nam ngoai pham vi (viettel,vina,mobi)
                {
                    WMT.AddMessage(WMO.UserID, "Tong dai 6x72 chi ho tro cho cac thue bao Viettel, Vinaphone, Mobifone. Chi tiet xem tai www.6x72.com.vn", SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                    WMT.Insert();
                }
                return;
            }
            else
            {
                string strError = "De tai bai hat: Soan tin NC TenBaiHat gui 6072\nDe tang bai hat cho ban be: Soan tin NC TenBaiHat SoDienThoaiNhan gui 6072\nChi tiet tai: www.6x72.com.vn";
                WMT.AddMessage(WMO.UserID, strError, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();
                return;
            }           
        }
        private string GetReturnMsg(string[] msg)
        {
            string strvalue = "";
            string code = EnCode(msg[0]);
            code = code.Replace("=", ".");            
            if (WMO.ServiceID.Replace("04", "") == "6072")//6072//Tải 1 bài hát
            {
                strvalue += "Link tai bai hat: http://6x72.com.vn/Download/Muzik/?type=1&code=" + code;
            }
            else if (WMO.ServiceID.Replace("04", "") == "6772")//6772//Tải bộ nhac
            {
                strvalue += "Link tai bo nhac: http://6x72.com.vn/Download/Muzik/?type=2&code=" + code;
            }
            else
            {
                strvalue = "De tai bai hat: Soan tin NC TenBaiHat gui 6072\nDe tang bai hat cho ban be: Soan tin NC TenBaiHat SoDienThoaiNhan gui 6072\nChi tiet tai: www.6x72.com.vn";
            }
            return strvalue;
        }
        private string EnCode(string str)
        {
            byte[] b = null;
            string ECString = null;
            b = System.Text.Encoding.Unicode.GetBytes(str);
            ECString = Convert.ToBase64String(b);
            return ECString;
        }        
        
        private int InsertMT(string strconnec,int mo_id, string User_id, string ServiceID, string CommandCode, string Message, int RequestID, int MsgType, int ContentType)
        {
            int val = 0;
            string reqSQL = "INSERT INTO SMS_MT(MO_ID,UserID,ReceiverUserID,ServiceID,CommandCode,Message,RequestID,MsgType,ContentType,RequestTime)";
            reqSQL += " VALUES(@MO_ID,@UserID,@ReceiverUserID,@ServiceID,@CommandCode,@Message,@RequestID,@MsgType,@ContentType,@RequestTime)";
            try
            {
                SqlConnection conn = new SqlConnection(strconnec);
                conn.Open();
                SqlCommand oCommand = new SqlCommand(reqSQL, conn);
                oCommand.Parameters.Clear();
                oCommand.Parameters.AddWithValue("@MO_ID", mo_id);
                oCommand.Parameters.AddWithValue("@UserID", User_id);
                oCommand.Parameters.AddWithValue("@ReceiverUserID", "");
                oCommand.Parameters.AddWithValue("@ServiceID", ServiceID);
                oCommand.Parameters.AddWithValue("@CommandCode", CommandCode);
                oCommand.Parameters.AddWithValue("@Message", Message);
                oCommand.Parameters.AddWithValue("@RequestID", RequestID);
                oCommand.Parameters.AddWithValue("@MsgType", MsgType);
                oCommand.Parameters.AddWithValue("@ContentType", ContentType);
                oCommand.Parameters.AddWithValue("@RequestTime", DateTime.Now);
                oCommand.ExecuteNonQuery();
                conn.Close();
                val = 1;
            }
            catch (Exception myErr)
            {
                val = 0;
            }
            return val;
        }        
        
        private string CheckTelco(string strnumber)
        {
            string str2 = strnumber.Substring(1, 2);
            string str3 = strnumber.Substring(1, 3);
            //viettel
            if (str2 == "97" || str2 == "98" || str3 == "163" || str3 == "164" || str3 == "165" || str3 == "166" || str3 == "167" || str3 == "168" || str3 == "169")
            {
                return "VIETTEL";
            }           
            //vina
            if (str2 == "91" || str2 == "94" || str3 == "123" || str3 == "125" || str3 == "127" || str3 == "129")
            {
                return "VINA";
            }           
            //mobi
            if (str2 == "90" || str2 == "93" || str3 == "120" || str3 == "121" || str3 == "122" || str3 == "126" || str3 == "128")
            {
                return "MOBI";
            }
            return "";
        }
        
    }
}